<?php

################################################################################################################
Function show_templates_list($tid=FALSE)
{
	$table_templates = $_SESSION['prefix']."templates";
	$req = dbquery("SELECT * FROM $table_templates WHERE 1");
  
	Print("<select name=\"tmp\"");
	while ($line = @mysql_fetch_array($req))
	{
		if ($tid == $line['tid'])
			Print("<option value=\"$tid\" SELECTED>$line[name]</option>");
		else
			Print("<option value=\"$line[tid]\">$line[name]</option>");
	}
	Print("</select>");
}

################################################################################################################
Function show_templates($start, $limit)
{
	$table_templates = $_SESSION['prefix']."templates";
	$connection = dbconnect();
	$req = dbquery("SELECT tid,name,`type`,`version`,`default` FROM $table_templates WHERE 1 ORDER BY tid ASC LIMIT $start,$limit", $connection);
	if (@mysql_num_rows($req) == 0)
	{
		mysql_close($connection);
		return (-1);
    }
   
	while ($line = @mysql_fetch_array($req))
    {		
			Print("
				<thead>
				<tr class=\"blue\">
					<td align=left><font color=green>$line[tid]</font></td>
					<td align=left><font color=darkblue><b>$line[name]</b></font></td>
					<td align=left valign=top>$line[type]</td>
					<td align=left valign=top>$line[version]</td>
					<td align=center valign=top>
			");
				if ($line['default'] == "ok")
					Print("<img src=\"$_SESSION[path_images]/valid.png\">");
			Print("
					</td>
					<td align=center>
			");
			
			if ($line['type'] != "system")
			{
				Print("
					<a href=\"ipage.php?height=190&act=edit&tid=$line[tid]\" class=\"thickbox\" title=\"<font color=royalblue><b>Edition template</b></font>\"><img src=\"$_SESSION[current_path]/images/template_edit.png\" width=16 height=16 title=\"Modifier le template $line[name]\"></a> ' 
					<a href=\"ipage.php?height=200&act=delete&tid=$line[tid]\" class=\"thickbox\" title=\"<font color=red><b>Suppression template</b></font>\"><img src=\"$_SESSION[current_path]/images/template_delete.png\" width=16 height=16 title=\"Supprimer le template $line[name]\"></a>
				");
			}
			
			if (($line['type'] == "system") && (is_root($_SESSION['login'], $connection)))
			{
				Print("
					<a href=\"ipage.php?height=190&act=edit&tid=$line[tid]\" class=\"thickbox\" title=\"<font color=royalblue><b>Edition module</b></font>\"><img src=\"$_SESSION[current_path]/images/template_edit.png\" width=16 height=16 title=\"Modifier le template $line[name]\"></a>
				");
			}
				
			Print("						
					</td>
				</tr>
				</thead>
			");
	}
	mysql_close($connection);
	return;
}

################################################################################################################
Function show_new_templates()
{
	$tmps = get_new_templates();
	$nb = count($tmps);
	$i = 0;
	if ($tmps != -1)
	{
		while ($i < $nb)
		{
			$install_file = "../".$_SESSION['path_templates']."/".$tmps[$i]."/install_tmp.php";
			if (file_exists($install_file))
			{
				if (!chk_template($install_file))
				{
					$ndescription = "<font color=red><b>Erreur dans le fichier de configuration!</b></font>";
					$class = "red";
				}
				else
				{
					require ($install_file);					
					if (isset($description))
						$ndescription = $description;
					else
						$ndescription = "Design";
					$class = "blue";
				}
			}
			else
			{
				$ndescription = "<font color=red><b>Fichier de configuration n'existe pas!</b></font>";
				$class = "red";
			}
				
			Print("
				<thead>
					<tr class=\"$class\">
					<td align=left><font color=darkblue><b>$tmps[$i]</b></font></td>
					<td align=left valign=top>$ndescription</td>
					<td align=left valign=top>$version</td>
					<td align=center>
				");
					
				if ($class = "red")
					Print("<a href=\"ipage.php?height=200&act=add&tid=$tmps[$i]\" class=\"thickbox\" title=\"<font color=royalblue><b>Installer un template</b></font>\"><img src=\"$_SESSION[current_path]/images/template_add.png\" width=16 height=16 title=\"Installer le template $tmps[$i]\"></a>");
						
				Print("						
						</td>
					</tr>
				</thead>
			");
			$i++;
		}
	}
}

################################################################################################################
Function show_template_edit($tid)
{
	$tmp_infos = get_template_infos($tid);
	
	Print("
		<div id=\"box\">
			<h3>$tmp_infos[name]</h3>
			<form action=\"templates.php\" method=POST>
			<input type=hidden name=\"act\" value=\"edit_tmp\">
			<input type=hidden name=\"tid\" value=\"$tid\">
			<table cellpadding=0 cellspacing=0 border=0 width=100%>
				<tr>
					<td width=30%><b><font color=royalblue>TID:</font></b></td>
					<td><font color=#4B4B4B>$tid</font></td>
				</tr>
				<tr>
					<td width=30%><b><font color=royalblue>Nom:</font></b></td>
					<td><input type=text name=\"name\" value=\"$tmp_infos[name]\"></td>
				</tr>
			</table>
			<hr color=#4B4B4B>
			<table cellpadding=0 cellspacing=0 border=0 width=100%>
				<tr>
					<td width=30%><b><font color=royalblue>Par d&eacute;faut:</font></b></td>");
				if ($tmp_infos['default'] == "ok")
					Print("<td><font color=green><b>Par d&eacute;faut</b></font> <img src=\"$_SESSION[path_images]/valid.png\">");
				else
					Print("<td><img src=\"$_SESSION[path_images]/stop.png\"> - <input type=radio name=\"default\" value=\"ok\"> Activer par d&eacute;faut</td>");
				Print("</tr>
			</table>
			<table cellpadding=0 cellspacing=0 border=0 width=100%>
				<tr>
					<td align=right><input type=submit value=\"Modifier\"></td>
				</tr>
			</table>
		</form>
		</div>
	");
	return;
}

################################################################################################################
Function show_template_add($tmp)
{
	Print("
		<br><br><br>
		<center>
			<table cellpadding=0 cellspcing=0 border=0 width=100%>
				<tr>
					<td width=30% align=right><img src=\"$_SESSION[current_path]/images/wait_1.gif\"></td>
					<td width=70% align=left><b>Installation du template en cours, veuillez patienter...</b></td>
				</tr>
			</table>
		</center>
	");
	$get_result = install_template($tmp);
	Print("<script language=JavaScript>setTimeout(\"document.location = 'templates.php?gr=$get_result'\", 3000)</script>");
	return;
}

################################################################################################################
Function show_template_delete($tid)
{
	$template_infos = get_template_infos($tid);
	
	Print("
		<div id=\"box\">
			<h3>
			<table border=0>
				<tr>
					<td width=40><img src=\"$_SESSION[path_images]/large/delete_1.png\" width=40 height=40></td>
					<td><font size=3>Supprimer le template $template_infos[name]</font></td>
				</tr>
			</table>
			</h3>
			<center>
				<br>
				<font color=\"red\"><b>Voulez vous continuer ?</b></font>
				<br>
				<table border=0 width=100%>
					<tr>
						<td width=50% align=right>
							<form action=\"templates.php\" method=POST>
								<table>
									<tr>
										<td align=right><input type=submit value=\"Annuler\"></td>
									</tr>
								</table>
							</form>
						</td>
						<td>
							<form action=\"templates.php\" method=POST>
							<input type=hidden name=\"act\" value=\"delete_tmp\">
							<input type=hidden name=\"tid\" value=\"$tid\">
							<table>
								<tr>
									<td><input type=submit value=\"Supprimer\"></td>
								</tr>
							</table>
							</form>
						</td>
					</tr>
				</table>
			</center>
		</div>
	");
	return;
}

?>